<?php
class Admin_Model_Pages extends Zend_Db_Table {
	protected $_name = 'pages';
	protected $_primary = 'id';
	public function getAll($arrParam = 1, $order = 'desc') {
		$db = Zend_Registry::get ( 'connectDb' );
		
		$select = $db->select ()->from ( array ('p' => 'pages' ) )
		->where('p.id_cate=?',$arrParam)
			->join ( array ('u' => 'users' ), 'p.id_user = u.id', array ('username' => 'u.username' ) );
		
		if ($order != "desc")
			$select = $select->order ( "p.time_create" );
		else
			$select = $select->order ( "p.time_create desc" );
		$stmt = $select->query ();
		$result = $stmt->fetchAll ();
		return $result;
	}
	public function getPageByUser($idUser,$arrParam = 1 ,$order = 'desc') {
		$db = Zend_Registry::get ( 'connectDb' );
	
		$select = $db->select ()->from ( array ('p' => 'pages' ) )
		->where('p.id_cate=?',$arrParam)
		->where("id_user =?",$idUser)
		->join ( array ('u' => 'users' ), 'p.id_user = u.id', array ('username' => 'u.username' ) );
	
		if ($order != "desc")
			$select = $select->order ( "p.time_create" );
		else
			$select = $select->order ( "p.time_create desc" );
		$stmt = $select->query ();
		$result = $stmt->fetchAll ();
		return $result;
	}
	public function updatePage($arrParam) {
		$data = array (
				'title' => $arrParam ['title'],
				'image' => $arrParam ['image'],
				'desription' => $arrParam ['description'],
				'category_id' => $arrParam ['category_id'],
				'content' => $arrParam ['content'] 
		);
		$where = 'id = ' . $arrParam ['page_id'];
		$this->update ( $data, $where );
	}
	public function addPage($arrParam) {
		$row = $this->fetchNew ();
		$row->title = $arrParam ['title'];
		$row->time = $arrParam ['time'];
		$row->location = $arrParam ['location'];
		$row->image = $arrParam ['image'];
		$row->content = $arrParam ['content'];
		$row->contact = $arrParam ['contact'];
		$row->phone = $arrParam ['phone'];
		$row->email = $arrParam ['email'];
		$row->id_user = $arrParam ['id_user'];
		$row->id_cate = $arrParam ['id_cate'];
		$row->description = $arrParam ['description'];
		$row->timestudy = $arrParam ['timestudy'];
		$row->cost = $arrParam ['cost'];
		$row->time_create = date ( "Y-m-d H:i:s" );
		$row->save ();
	}
	public function editPage($arrParam) {
		$where = 'id = ' . $arrParam ['id'];
		
		$row = $this->fetchRow ( $where );
		$row->title = $arrParam ['title'];
		$row->time = $arrParam ['time'];
		$row->location = $arrParam ['location'];
		$row->image = $arrParam ['image'];
		$row->content = $arrParam ['content'];
		$row->contact = $arrParam ['contact'];
		$row->phone = $arrParam ['phone'];
		$row->email = $arrParam ['email'];
		$row->id_cate = $arrParam ['id_cate'];
		$row->description = $arrParam ['description'];
		$row->timestudy = $arrParam ['timestudy'];
		$row->cost = $arrParam ['cost'];
		$row->save ();
	}
	public function savePage($arrParam = null, $options = null) {
		if ($options ['task'] == 'add') {
			$row = $this->fetchNew ();
			$row->category_id = $arrParam ['category_id'];
			$row->title = $arrParam ['title'];
			$row->image = $arrParam ['image'];
			$row->desription = $arrParam ['description'];
			$row->content = $arrParam ['content'];
			$row->timepost = date ( "Y-m-d H:i:s" );
			$row->save ();
		}
		
		if ($options ['task'] == 'edit') {
			
			echo __METHOD__;
			echo '<pre>';
			print_r ( $arrParam );
			echo '</pre>';
			$where = 'id = ' . $arrParam ['id'];
			
			$row = $this->fetchRow ( $where );
			$encode = new Zendvn_Encode ();
			$row->user_name = $arrParam ['user_name'];
			
			if (! empty ( $arrParam ['password'] )) {
				$row->password = $encode->password ( $arrParam ['password'] );
			}
			$row->email = $arrParam ['email'];
			$row->group_id = $arrParam ['group_id'];
			$row->first_name = $arrParam ['first_name'];
			$row->last_name = $arrParam ['last_name'];
			$row->birthday = $arrParam ['birthday'];
			$row->status = $arrParam ['status'];
			$row->sign = $arrParam ['sign'];
			$row->user_avatar = $arrParam ['user_avatar'];
			
			$row->save ();
		}
	}
	public function getItem($arrParam = null, $options = null) {
		if ($options ['task'] == 'admin-info' || $options ['task'] == 'admin-edit') {
			$db = Zend_Registry::get ( 'connectDb' );
			// $db = Zend_Db::factory($adapter,$config);
			@$select = $db->select ()->from ( 'users as u' )->joinLeft ( 'user_group as g', 'u.group_id = g.id', array (
					'group_name' 
			) )->where ( 'u.id = ?', $arrParam ['id'], INTEGER );
			
			$result = $db->fetchRow ( $select );
		}
		
		if ($options ['task'] == 'delete') {
			$where = 'id = ' . $arrParam ['id'];
			$result = $this->fetchRow ( $where )->toArray ();
		}
		return $result;
	}
	public function deletePage($arrParam = null) {
		$where = $this->getAdapter ()->quoteInto ( 'id = ?', $arrParam ["id"] );
		$this->delete ( $where );
	}
	public function getPage($arrParam) {
		$se = $this->select ();
		$se->where ( "id= ?", $arrParam );
		
		return $this->fetchRow ( $se )->toArray ();
	}
}